package sshFile;


import org.apache.commons.net.telnet.TelnetClient;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;


public class TelnetTool {
    private TelnetClient telnet = new TelnetClient();
    private PrintStream out;
    private InputStream in;
    private char prompt = '$';
    public InputStreamReader inre;

    // 闁哄拋鍣ｉ敓鑺ユ皑閺併倝骞嬫搴ｆ尝闁哄鎷�
    public void runOmc(String ip, int port, String user, String password, String fileName) {
        try {
            telnet.connect(ip, port);
            System.out.println("telnet执行");

            out = new PrintStream(telnet.getOutputStream(), false, "utf-8");

            // 闁哄秷顫夊畵涔簅ot闁烩偓鍔嶉崺娑氭媼閸撗呮瀭缂備焦鎸诲顐ょ箔閿燂拷
            this.prompt = user.equals("root") ? '#' : '$';
            in = telnet.getInputStream();
            inre = new InputStreamReader(in, "gbk");
            Thread.sleep(5000);
            write(user);
            //readUntil("Password:");
            write(password);
            System.out.println("OMC登陆成功");

            //	write("S_ACTIVATE:FILE=\""+fileName+"\";");


//			return readUntil("记录数");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public boolean testConn(String ip, int port) {
        boolean flag = false;
        try {
            telnet.connect(ip, port);
            flag = true;
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 闁谎嗩嚙缂嶏拷 * * @param user * @param password
     */
    public boolean login(String ip, int port, String user, String password) {
        boolean flag = false;
        try {
            telnet.connect(ip, port);
            out = new PrintStream(telnet.getOutputStream());
            in = telnet.getInputStream();
            //readUntil("login:");
            write(user);
            //readUntil("Password:");
            write(password);
            //readUntil(prompt + " ");
            flag = true;
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 闁告碍鍨瑰ú浼村冀閸パ冪岛闂侇偂绀侀幊鈩冪閵堝懐鎽熺紒妤嬬細鐟曪拷 * * @param command * @return
     */
    public String sendCommand(String command) {
        try {
            System.out.println("开始输入命令: " + command);
            write(command);
            System.out.println("输入完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String sendCommanddu(String command) {
        try {
            System.out.println("开始输入命令: " + command);
            write(command);
            System.out.println("输入完毕");
            String in = readUntil("记录数");
            String[] one = {"[D\u001B", "...... \b \b|\b \b\b \b\b \b\b \b\b \b\b \b\b \b"};
            String[] two = {"\u001B[2K\u001B[D", "\u001B[2K\u001B[A\u001B[2K"};
			return tihuan(in, one, two);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String tihuan(String in, String[] one, String[] two) {
        for (String a : one) {
            in = in.replace(a, "");
        }
        for (String b : two) {
            in = in.replace(b, System.getProperty("line.separator"));
        }
        return in;
    }


    /**
     * 闁告劖鐟﹂幖閿嬫媴閿燂拷 * * @param value
     */
    public void write(String value) {
        try {
            out.println(value);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 闁稿繑濞婂Λ瀛樻交閻愭潙澶�
     */
    public void disconnect() {
        try {
            System.out.println("开始断开");
            telnet.disconnect();
            System.out.println("已断开");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 閻犲洩顕цぐ鍥礆閸℃锟界晫绱掗幘瀵镐函 * * @param pattern * @return
     */
    public String readUntil(String pattern) {
        try {
            char lastChar = pattern.charAt(pattern.length() - 1);
            StringBuffer sb = new StringBuffer();


            char ch = (char) inre.read();
            while (true) {
                sb.append(ch);
                if (ch == lastChar) {
                    if (sb.toString().endsWith(pattern)) {
                        return sb.toString();
                    }
                }
                ch = (char) inre.read();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 闁告劖鐟ラ崣鍡涘棘閸ワ附顐�
     * */
    public static void doWrite(String path, String data) {
        try {
            System.out.println("开始写txt");
            File file = new File(path);
            Writer ps = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));

            ps.write(data + "\r\n");

            ps.flush();
            ps.close();
            System.out.println("写完");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {
            System.out.println("开始执行ash");
            String ip = "132.226.223.79";
            int port = 21123;
            String user = "huqiao.zte";
            String password = "Sophie@135";
            String setamo = "set amo:moi= jtuxpkfr-2";
            String comm = "FDD_QUERYCELLSTATUS:SUBNET=321216,NE=346769,EUTRANCELLID=1;";
            TelnetTool telnet = new TelnetTool();
            telnet.runOmc(ip, port, user, password, null);
            System.out.println("开始倒计时");
            Thread.sleep(1000);
            telnet.sendCommand(setamo);
            System.out.println("切网元");
            Thread.sleep(1000);
            String order = telnet.sendCommanddu(comm);
            String path = "C:\\new\\omc.txt";
            telnet.disconnect();

            doWrite(path, order);

            System.out.println("命令执行结果:" + order);
            //	 CreateCsv.createCSVtool(path);
			
			/* File txtfile=new File(path);
			 if(txtfile.exists()){
			 txtfile.delete();
			 }*/

            System.out.println("end ...");
        } catch (Exception e) {
            System.out.println("e:" + e.getMessage());
            e.printStackTrace();
        }
    }
}